import logging

from appium import webdriver

from test_lubo.app_PO4.page.base_page import BasePage
from test_lubo.app_PO4.page.main_page import MainPage


class App(BasePage):
    def start(self):
        if self.driver == None:
            logging.info("driver = None,创建一个新的driver")
            desired_caps = {}
            desired_caps["platformName"] = "Android"
            desired_caps["platformVersion"] = "6.0.1"
            desired_caps["deviceName"] = "127.0.0.1:7555"
            desired_caps["appPackage"] = "com.tencent.wework"
            desired_caps["appActivity"] = ".launch.LaunchSplashActivity"
            desired_caps["noReset"] = "True"
            desired_caps["ensureWebviewsHavePages"] = True
            desired_caps["unicodeKeyBoard"] = "True"  # 默认输入状态为英文输入,使其可以进行中文输入
            desired_caps["resetKeyBoard"] = "True"  # 默认输入状态为英文输入,使其可以进行中文输入
            self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
            self.driver.implicitly_wait(5)
        else:
            logging.info("复用driver")
            #复用driver，启动app,launch_app()不需要传递任何参数，只需启动driver里设置好的activity
            # self.driver.start_activity(package,activity)可以启动任何新的activity，而不局限于已经设置好的
            self.driver.launch_app()
        return self

    def restart(self):
        #先关闭app，再打开app,即重启
        self.driver.close_app()
        self.driver.launch_app()

    def stop(self):
        self.driver.quit()

    def goto_main(self):
        return MainPage(self.driver)

